package track2.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;


import track2.context.Config;
import track2.linear.LRFeatures;
import track2.linear.LRformula;
import track2.linear.LinearRegression;
import track2.result.Result;
import track2.result.ResultFileTool;
import track2.newBayes.*;

/**
 * Entry class for the system Start testing work here
 * 
 * plan 1: naive bayes model plan 1rd: id naive bayes model plan 2: svd plan 3:
 * linear regression plan 4: knn plan 5: feature selection plan 6: ensemble
 * models
 * 
 * 
 * @author yijia, lingjuan
 * 
 */
public class TestEngine {

	public static void naivebayes() {
		NaiveBayesFeatures feature = TrainClassifier.train();
		// NaiveBayesFeatures feature = NaiveBayesFeatures.getObject();
		NaiveBayesFeatures.putObject(feature);

		// test1
		Result result = TestClassifier.test(feature, Config.testFilePath);
		ResultFileTool.outputResult(result, Config.nbTestResultPath);

		// test2
		// result = TestClassifier.test(feature, Config.sampleTestFilePath);
		// ResultFileTool.outputResult(result, Config.nbSampleTestResultPath);

		// NaiveBayesFeatures.putObject(feature);

	}

	public static void idNaivebayes() throws IOException, ClassNotFoundException {
		IdFeatures feature = IdClassifier.train();
		IdFeatures.putObject(feature);

		// test1
		Result result = IdTestClassifier.test(feature, Config.testFilePath);
		ResultFileTool.outputResult(result, Config.idnbTestResultPath);

		// test2
		// result = IdTestClassifier.test(feature, Config.sampleTestFilePath);
		// ResultFileTool.outputResult(result, Config.idnbSampleTestResultPath);

	}

	public static void lr() {
		LRFeatures feature = LinearRegression
				.calcMeanFeatures(Config.trainingFilePath);
		LRFeatures.putObject(feature, Config.lrFeaturePath);

		LinearRegression.outputMeanFeatures(feature, Config.trainingFilePath,
				Config.lrTrainFilePath);

		boolean[] choose = { true, true, true, true, true, true, true, true };
		LinearRegression.outputMeanFeatures(feature, Config.trainingFilePath,
				Config.lrTrainFilePath);

		LRformula formula = LinearRegression.trainLinearModel(choose);
		formula.print();
		LRformula.putObject(formula);

		Result result = LinearRegression.testLinearModel(choose, feature,
				formula, Config.testFilePath);

		ResultFileTool.outputResult(result, Config.lrTestFilePath);
	}

	public static void test() throws IOException, ClassNotFoundException {
		File file = new File(Config.idNaiveBayesFeaturePath);
		FileInputStream fs = new FileInputStream(file);
		ObjectInputStream os = new ObjectInputStream(fs);
		IdFeatures feature = (IdFeatures) os.readObject();
		Result result = IdTestClassifier.test(feature, Config.testFilePath);
		ResultFileTool.outputResult(result, Config.idnbTestResultPath);
	}

	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		// run naive bayes model
		naivebayes();

		 //idNaivebayes();
		//idNaivebayes();

		// lr();
		 //test();

	}

}
